/*
Continuous Testing Toolkit
Copyright (C) 2009 Paul R. Holser, Jr.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package org.infinitest.toolkit.filter;

import java.util.List;
import java.util.Properties;

import org.junit.runner.Runner;
import org.junit.runner.manipulation.NoTestsRemainException;

class FilteringRunnerHelper {
    static final String TEST_TYPE_PROPERTY_KEY = "org.infinitest.filter.types";

    static boolean hasPrintedMessage;

    static {
        new FilteringRunnerHelper();
    }

    private FilteringRunnerHelper() {
        // nothing to do here
    }

    static TestTypeFilter buildFilter(Class<?> clazz, Properties properties) {
        List<String> testTypes = TestType.testTypesFrom(properties);
        printMessage(testTypes);
        return new TestTypeFilter(clazz, testTypes);
    }

    static void filterRunner(Runner runner, Class<?> clazz, Properties properties) {
        try {
            buildFilter(clazz, properties).apply(runner);
        }
        catch (NoTestsRemainException ex) {
            System.out.println("No tests to run.");
        }
    }

    private static void printMessage(List<String> testType) {
        if (!hasPrintedMessage) {
            System.out.println("Filtering for test types: " + testType.toString() + "");
            hasPrintedMessage = true;
        }
    }
}
